import click
import asyncio
from rich.console import Console
from ..core.manager import PackageManager

console = Console()

@click.group()
@click.option('--config', type=click.Path(), help='配置文件路径')
@click.pass_context
def cli(ctx, config):
    """包管理器命令行工具"""
    ctx.obj = PackageManager(config)

@cli.command()
@click.argument('package_name')
@click.option('--version', '-v', default="latest", help='包版本')
@click.pass_obj
def install(manager: PackageManager, package_name: str, version: str):
    """安装包"""
    asyncio.run(manager.install(package_name, version))

@cli.command()
@click.argument('package_name')
@click.option('--force', '-f', is_flag=True, help='强制卸载')
@click.pass_obj
def uninstall(manager: PackageManager, package_name: str, force: bool):
    """卸载包"""
    asyncio.run(manager.uninstall(package_name, force))

@cli.command()
@click.argument('package_name', required=False)
@click.pass_obj
def update(manager: PackageManager, package_name: str):
    """更新包"""
    asyncio.run(manager.update(package_name))

@cli.command()
@click.option('--details', '-d', is_flag=True, help='显示详细信息')
@click.pass_obj
def list(manager: PackageManager, details: bool):
    """列出已安装的包"""
    manager.list_packages(details)

@cli.command()
@click.argument('query')
@click.pass_obj
def search(manager: PackageManager, query: str):
    """搜索包"""
    asyncio.run(manager.search(query))

def main():
    cli(obj=None)

if __name__ == '__main__':
    main()